﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.gates {
    import Engine.Managers.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import __AS3__.vec.*;
    import Display.*;
    import Display.UpgradesUI.*;
    import Modules.stats.experiments.*;
    import Modules.crew.*;
    import Display.GateUI.*;
    import Modules.stats.types.*;

    public class UpgradeCrewGate extends CrewGate {

        public static const GATE_NAME:String = "pre_upgrade";

        public function UpgradeCrewGate(_arg1:String){
            super(_arg1);
        }
        override public function displayGate(_arg1:String=null, _arg2:String=null):void{
            var _local14:String;
            var _local15:Object;
            var _local16:Player;
            var _local17:Player;
            m_dialog = null;
            var _local3:Array = new Array();
            var _local4:Crew = Global.crews.getCrewById(m_targetObjectId);
            var _local5:Vector.<String> = this.getCrewNames();
            var _local6:int = Global.player.inventory.getItemCountByName("crew_bonus");
            var _local7:Number = m_keys[REQUIRED_CREW];
            var _local8:int;
            while (_local8 < _local7) {
                _local14 = (((_local4) && ((_local4.count > _local8)))) ? _local4.list[_local8] : null;
                _local14 = normalizeUid(_local14);
                _local15 = new Object();
                _local16 = (_local14) ? Global.player.findFriendById(_local14) : null;
                _local17 = Global.player.findFriendById("-1");
                if (_local14){
                    _local15.url = (_local16) ? _local16.snUser.picture : _local17.snUser.picture;
                    _local15.count = 1;
                    _local15.amountNeeded = 1;
                    _local15.footerText = (_local16) ? _local16.firstName : _local17.firstName;
                } else {
                    if (_local6 > 0){
                        _local15.url = _local17.snUser.picture;
                        _local15.count = 1;
                        _local15.amountNeeded = 1;
                        _local15.footerText = ZLoc.t("Items", "crew_bonus_friendlyName");
                        _local6--;
                    } else {
                        _local15.displayObjectClass = EmbeddedArt.hud_no_profile_pic;
                        _local15.cost = getCrewCost(m_unlockItemName, GATE_NAME);
                        _local15.count = 0;
                        _local15.amountNeeded = 1;
                        _local15.buyCallback = this.buyCrew;
                        _local15.footerText = ZLoc.t("Dialogs", "fillPosition");
                    };
                };
                _local15.name = _local5[_local8];
                _local15.backgroundClass = EmbeddedArt.crewFriendBackground;
                _local3.push(_local15);
                _local8++;
            };
            m_cellFactory = new KeyCellCrewFactory(_local3);
            var _local9:Item = Global.gameSettings.getItemByName(m_unlockItemName);
            var _local10:String = ZLoc.t("Dialogs", "CrewGateDialogTitle", {item:_local9.localizedName});
            var _local11:Item = Global.gameSettings.getItemByName(m_unlockItemName);
            var _local12:String = _local11.type;
            if (m_dialog == null){
                if (_local12.toLowerCase() == "municipal"){
                    m_dialog = UI.displayMunicipalUpgradesDialog(m_unlockItemName, _local3, checkForKeys, gateFinishHandler, this.buyRemainingCrew, m_targetObjectId);
                };
            };
            StatsManager.sample(100, StatsCounterType.DIALOG, StatsKingdomType.ACTIVE_BUILDING, StatsPhylumType.VIEW, _local9.upgrade.newItemName);
            var _local13:int = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_CREW_POPUP_MFS);
            if (_local13 == ExperimentDefinitions.TRAY_POPUP_MFS){
                FrameManager.preloadTray(("crew.php?mId=" + m_targetObjectId.toString()), true);
            };
        }
        override protected function getCrewNames():Vector.<String>{
            var result:* = null;
            var item:* = null;
            var gatesxml:* = null;
            var member:* = null;
            var nameToken:* = null;
            var amount:* = NaN;
            var name:* = null;
            var i:* = 0;
            result = new Vector.<String>();
            item = Global.gameSettings.getItemByName(m_unlockItemName);
            gatesxml = item.gatesXml.gate.(@name == GATE_NAME);
            for each (member in gatesxml["key"]["member"]) {
                nameToken = member.@name;
                amount = member.@amount;
                name = ZLoc.t("Items", nameToken);
                i = 0;
                while (i < amount) {
                    result.push(name);
                    i = (i + 1);
                };
            };
            return (result);
        }
        override protected function askForCrew(_arg1:String=null):void{
            var _local2:Item = Global.gameSettings.getItemByName(m_unlockItemName);
            StatsManager.sample(100, StatsCounterType.DIALOG, StatsKingdomType.ACTIVE_BUILDING, StatsPhylumType.HIRE, _local2.upgrade.newItemName);
            super.askForCrew(GATE_NAME);
        }
        override protected function buyCrew(_arg1:Number):Boolean{
            var _local4:MapResource;
            var _local2:Boolean;
            var _local3:Item = Global.gameSettings.getItemByName(m_unlockItemName);
            StatsManager.sample(100, StatsCounterType.DIALOG, StatsKingdomType.ACTIVE_BUILDING, StatsPhylumType.FILL_ONE, _local3.upgrade.newItemName);
            if (Global.player.canBuyCash(_arg1, true)){
                _local2 = true;
                GameTransactionManager.addTransaction(new TBuyCrew(m_targetObjectId, 0, _arg1, GATE_NAME));
                if (checkForKeys() == true){
                    (m_dialog as MunicipalUpgradesDialog).activateFinishButton();
                };
                _local4 = (Global.world.getObjectById(m_targetObjectId) as MapResource);
                if (_local4 != null){
                    _local4.refreshArrow();
                };
            };
            return (_local2);
        }
        protected function buyRemainingCrew(_arg1:Array):Boolean{
            var _local7:int;
            var _local8:MapResource;
            var _local2:Boolean;
            var _local3:Number = getCrewCost(m_unlockItemName, GATE_NAME);
            var _local4:Number = (_arg1.length - CrewGate.getFilledPositions(_arg1));
            var _local5:Number = (_local3 * _local4);
            var _local6:Item = Global.gameSettings.getItemByName(m_unlockItemName);
            StatsManager.sample(100, StatsCounterType.DIALOG, StatsKingdomType.ACTIVE_BUILDING, StatsPhylumType.FILL_ALL, _local6.upgrade.newItemName);
            if (Global.player.canBuyCash(_local5, true)){
                Global.player.cash = (Global.player.cash - _local5);
                _local2 = true;
                _local7 = 0;
                while (_local7 < _local4) {
                    Global.crews.addCrewById(m_targetObjectId, "-1");
                    _local7++;
                };
                GameTransactionManager.addTransaction(new TBuyRemainingCrew(m_targetObjectId, GATE_NAME));
                _local8 = (Global.world.getObjectById(m_targetObjectId) as MapResource);
                if (_local8 != null){
                    _local8.refreshArrow();
                };
            };
            return (_local2);
        }

    }
}//package Classes.gates 
